package com.foin.city.widget.navigation;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import com.foin.city.R;
import com.foin.city.utils.DisplayUtil;


/**
 * @author Foin
 * @version 1.0
 * @time 2017/4/19
 * @desc 默认导航
 */
public class NavigationBar<D extends NavigationBar.Builder.NavigationBarParams> extends AbsNavigationBar<NavigationBar.Builder.NavigationBarParams> {

    public NavigationBar(NavigationBar.Builder.NavigationBarParams absParams) {
        super(absParams);
    }

    @Override
    public int bindLayoutId() {
        return R.layout.view_navigation_bar;
    }

    /**
     * 设置标题栏的属性
     */
    @Override
    public void applyView() {
        if (getAbsParams().navigationIcon != 0) {
            setImageResource(R.id.back_icon, getAbsParams().navigationIcon);
        }
        if (getAbsParams().backClickListener != null) {
            setOnClickListener(R.id.back_icon, getAbsParams().backClickListener);
        }
        findViewById(R.id.status_bar_view).setVisibility(getAbsParams().statusBarVisible);
        if (getAbsParams().statusBarVisible == View.VISIBLE) {
            LinearLayout.LayoutParams statusBarParams = (LinearLayout.LayoutParams) findViewById(R.id.status_bar_view).getLayoutParams();
            statusBarParams.height = DisplayUtil.getNotificationHeight();
            findViewById(R.id.status_bar_view).setLayoutParams(statusBarParams);
            if (getAbsParams().statusBarBackResource != 0) {
                findViewById(R.id.status_bar_view).setBackgroundResource(getAbsParams().statusBarBackResource);
            }
        }
        setText(R.id.title_text, getAbsParams().title);
        if (getAbsParams().titleColor != 0) {
            setTextColor(R.id.title_text, getAbsParams().titleColor);
        }
        if (getAbsParams().menuText != null) {
            setText(R.id.menu_text, getAbsParams().menuText);
            if (getAbsParams().menuColor != 0) {
                setTextColor(R.id.menu_text, getAbsParams().menuColor);
            }
        } else {
            if (getAbsParams().menuIcon != 0) {
                setImageResource(R.id.menu_icon, getAbsParams().menuIcon);
            }
        }
        if (getAbsParams().menuClickListener != null) {
            setOnClickListener(R.id.menu_text, getAbsParams().menuClickListener);
            setOnClickListener(R.id.menu_icon, getAbsParams().menuClickListener);
        }
        findViewById(R.id.line).setVisibility(getAbsParams().bottomLineVisible);
        if (getAbsParams().toolbarBackResources != 0) {
            findViewById(R.id.toolbar_content).setBackgroundResource(getAbsParams().toolbarBackResources);
        }
    }


    public static class Builder extends AbsNavigationBar.Builder {

        public static NavigationBarParams params;

        public Builder(Context context) {
            super(context, null);
            params = new NavigationBarParams(context, null);
        }

        public Builder(Context context, ViewGroup parent) {
            super(context, parent);
            params = new NavigationBarParams(context, parent);
        }

        @Override
        public NavigationBar builder() {
            NavigationBar navigationBar = new NavigationBar(params);
            return navigationBar;
        }

        public NavigationBar.Builder setNavigationIcon(@DrawableRes int navigationIcon) {
            params.navigationIcon = navigationIcon;
            return this;
        }

        public NavigationBar.Builder setNavigationClickListener(View.OnClickListener clickListener) {
            params.backClickListener = clickListener;
            return this;
        }

        public NavigationBar.Builder setTitle(@StringRes int title) {
            return setTitle(params.context.getResources().getString(title));
        }

        public NavigationBar.Builder setTitle(String title) {
            params.title = title;
            return this;
        }

        public NavigationBar.Builder setTitleTextColor(@ColorInt int textColor) {
            params.titleColor = textColor;
            return this;
        }

        public NavigationBar.Builder setMenuIcon(@DrawableRes int menuIcon) {
            params.menuIcon = menuIcon;
            return this;
        }

        public NavigationBar.Builder setMenuText(@StringRes int menuText) {
            return setMenuText(params.context.getResources().getString(menuText));
        }

        public NavigationBar.Builder setMenuText(String menuText) {
            params.menuText = menuText;
            return this;
        }

        public NavigationBar.Builder setMenuTextColor(@ColorInt int menuTextColor) {
            params.menuColor = menuTextColor;
            return this;
        }

        public NavigationBar.Builder setMenuClickListener(View.OnClickListener clickListener) {
            params.menuClickListener = clickListener;
            return this;
        }

        public NavigationBar.Builder setToolbarBack(@DrawableRes int resId) {
            params.toolbarBackResources = resId;
            return this;
        }

        public NavigationBar.Builder showBottomShadow(int visible) {
            params.bottomLineVisible = visible;
            return this;
        }

        public NavigationBar.Builder showStatusBar(int visible) {
            params.statusBarVisible = visible;
            return this;
        }

        public NavigationBar.Builder setStatusBarBack(@DrawableRes int resId) {
            params.statusBarBackResource = resId;
            return this;
        }

        public static class NavigationBarParams extends AbsNavigationBar.Builder.AbsNavigationBarParams {
            private int statusBarVisible = View.GONE;
            private int statusBarBackResource = R.color.white;


            public int navigationIcon = R.drawable.icon_arrow_back_black;
            public View.OnClickListener backClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) context).finish();
                }
            };

            public String title;
            public int titleColor = 0xff333333;

            public int menuIcon;
            public String menuText;
            public int menuColor = 0xff666666;
            public View.OnClickListener menuClickListener;

            private int bottomLineVisible = View.VISIBLE;

            public int toolbarBackResources = R.color.white;


            public NavigationBarParams(Context context, ViewGroup parent) {
                super(context, parent);
            }
        }
    }

}
